﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class Account_Default : System.Web.UI.Page
{
    private List<DateTime> recordDay = new List<DateTime>();
    protected void Page_Load(object sender, EventArgs e)
    {
        ShowTarget();

    }
    protected void Page_Init(object sender, EventArgs e)
    {
        //Calendar1.SelectedDate = DateTime.Now.Date;
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
        SqlCommand cmd;
        SqlDataReader reader;



        try
        {
            conn.Open();

            cmd = new SqlCommand("SELECT date FROM Schedule WHERE UserId=@UserId", conn);
            cmd.Parameters.AddWithValue("@Userid", Page.User.Identity.Name);

            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                recordDay.Add((DateTime)reader["date"]);
                
            }
        
        }
        finally
        {
            conn.Close();
        }
        SqlDataSource1.SelectParameters["UserId"].DefaultValue = User.Identity.Name;
        SqlDataSource1.InsertParameters["UserId"].DefaultValue = User.Identity.Name;
        
    }
    protected void btSubmitInfo_Click(object sender, EventArgs e)
    {

        SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
        SqlCommand cmd = thisConnection.CreateCommand();

        try
        {
             //Open Connection
            thisConnection.Open();

            // Create INSERT statement with named parameters
            cmd.CommandText = @"INSERT  INTO UserInformation (UserId, Sex, Age, Height, Weight, TargetWeight) VALUES (@Userid, @Sex,@Age, @Height,@Weight,@TargetWeight)";

            string sex="M";
            if(rbFemale.Checked)
                sex="F";

            // Add Parameters to Command Parameters collection
            cmd.Parameters.AddWithValue("@Userid", Page.User.Identity.Name);
            cmd.Parameters.AddWithValue("@Sex", sex);
            cmd.Parameters.AddWithValue("@Age", tbAge.Text);
            cmd.Parameters.AddWithValue("@Height", tbHight.Text);
            cmd.Parameters.AddWithValue("@Weight", tbWeight.Text);
            cmd.Parameters.AddWithValue("@TargetWeight", tbTarget.Text);



            cmd.ExecuteNonQuery();
        }

    

        finally
        {
            // Close Connection
            thisConnection.Close();
            Response.Redirect("Default.aspx");

        }
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
        SqlCommand cmd;
        SqlDataReader reader;



        try
        {
            conn.Open();

            cmd = new SqlCommand("SELECT * FROM Schedule WHERE UserId=@UserId AND date=@date", conn);
            cmd.Parameters.AddWithValue("@Userid", Page.User.Identity.Name);
            cmd.Parameters.AddWithValue("@date", Calendar1.SelectedDate);

            reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {
                DetailsViewUpdate.Visible = true;
                DetailsViewInsert.Visible = false;

                
            }
            else
            {
                DetailsViewInsert.Visible  = true;
                DetailsViewUpdate.Visible = false ;
            }
        }
        finally
        {
            conn.Close();
        }
    }
    protected void DetailsViewInsert_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        DetailsViewUpdate.Visible = true;
        DetailsViewInsert.Visible = false;
        ShowTarget();
    }
    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        if(recordDay.Contains(e.Day.Date))
        {
            //e.Cell.BackColor = System.Drawing.Color.LightGreen;
            e.Cell.BorderWidth = 1;
            e.Cell.BorderColor = System.Drawing.Color.LightGreen;
        }
    }
    protected void btUpdate_Click(object sender, EventArgs e)
    {
        SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
        SqlCommand cmd = thisConnection.CreateCommand();

        try
        {
            //Open Connection
            thisConnection.Open();

            // Create INSERT statement with named parameters
            cmd.CommandText = @"Update top(1) UserInformation SET TargetWeight= @TargetWeight WHERE Userid= @Userid";

           
            cmd.Parameters.AddWithValue("@Userid", Page.User.Identity.Name);
            cmd.Parameters.AddWithValue("@TargetWeight",tbUpdateTarget.Text);



            cmd.ExecuteNonQuery();
        }



        finally
        {
            // Close Connection
            thisConnection.Close();
            divUpdateTarget.Visible = false;
            ShowTarget();
            tbUpdateTarget.Text = "";

        }
    }
    protected void lbUpdateTarget_Click(object sender, EventArgs e)
    {
        divUpdateTarget.Visible = true;
    }
    protected void ShowTarget()
    {

        SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
        SqlCommand cmd;
        SqlDataReader reader;



        try
        {
            thisConnection.Open();

            cmd = new SqlCommand("SELECT TargetWeight FROM UserInformation WHERE UserId=@UserId", thisConnection);
            cmd.Parameters.AddWithValue("@Userid", Page.User.Identity.Name);
            reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {
                Panel1.Visible = true;
                panelUserInfo.Visible = false;
                decimal targetWeight = 0;
                decimal nowWeight = 0;
                while (reader.Read())
                {
                    targetWeight = (decimal)reader["TargetWeight"];
                }
                reader.Close();

                cmd = new SqlCommand("SELECT  TOP 1* FROM Schedule WHERE UserId=@UserId Order by Date Desc ", thisConnection);
                cmd.Parameters.AddWithValue("@Userid", Page.User.Identity.Name);
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    nowWeight = (decimal)reader["weight"];
                }
                reader.Close();
                if (nowWeight == targetWeight)
                {
                    lbTarget.Text = string.Format("Congratulations, you have achieved your target weight {0} Kg !", targetWeight);
                }
                else
                {
                    decimal diff = nowWeight - targetWeight;
                    if (diff < 0) diff *= -1;

                    lbTarget.Text = string.Format(@"Now weight:{0}<br/>Target weight:{1}
<br/>Difference:{2}", nowWeight, targetWeight, diff);
                }
            }
            else
            {
                Panel1.Visible = false;
            }

        }
        finally
        {
            // Close Connection
            thisConnection.Close();

        }
    }
    protected void DetailsViewUpdate_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        ShowTarget();
    }
}